package com.xjlsoft.weather.service.impl;

import com.xjlsoft.weather.common.util.DataUtil;
import com.xjlsoft.weather.common.util.DateUtil;
import com.xjlsoft.weather.dto.DataAvgDto;
import com.xjlsoft.weather.entity.AerosolAvg;
import com.xjlsoft.weather.entity.GroundAvg;
import com.xjlsoft.weather.entity.GroundData;
import com.xjlsoft.weather.enums.AvgFlagEnum;
import com.xjlsoft.weather.enums.DataTypeEnum;
import com.xjlsoft.weather.mapper.AerosolAvgMapper;
import com.xjlsoft.weather.mapper.GroundAvgMapper;
import com.xjlsoft.weather.mapper.GroundDataMapper;
import com.xjlsoft.weather.mapper.RadiationAvgMapper;
import com.xjlsoft.weather.service.GroundAvgService;
import com.xjlsoft.weather.util.WeatherDataUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 地面数 平均数据 表 服务实现类
 * </p>
 *
 * @author zhangbanglong
 * @since 2020-08-19
 */
@Service
public class GroundAvgServiceImpl extends ServiceImpl<GroundAvgMapper, GroundAvg> implements GroundAvgService {

	@Autowired
	private GroundAvgMapper groundAvgMapper;

	
	@Autowired
	private GroundDataMapper groundDataMapper;

	

	@Override
	public Map<String, Object> getAvgTem(String year) {
		List<String> listDate = groundAvgMapper.getDayTimeStr(DataTypeEnum.Day.getValue(),year);
		List<String> listData = groundAvgMapper.getGroundAvgTem(year);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("times", listDate);
		map.put("datas", listData);
		return map;

	}
	
	public Map<String, Object> getMapByFilesDayMonWeekYear(DataAvgDto groundAvgDto){
			Map<String, Object> map = new HashMap<String, Object>();
			DataAvgDto dto = groundAvgDto;
			QueryWrapper<GroundAvg> query = Wrappers.query();
			if (StringUtils.isNotBlank(dto.getPrs())) {
				query.select(dto.getPrs());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("datas", list);
				return map;
			}

			if (StringUtils.isNotBlank(dto.getTem())) {
				query.select(dto.getTem());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("datas", list);
				return map;
			}
			if (StringUtils.isNotBlank(dto.getDpt())) {
				query.select(dto.getDpt());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("datas", list);
				return map;
			}

			if (StringUtils.isNotBlank(dto.getRhu())) {
				query.select(dto.getRhu());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("datas", list);
				return map;
			}
			if (StringUtils.isNotBlank(dto.getVap())) {
				query.select(dto.getVap());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("datas", list);
				return map;
			}

			if (StringUtils.isNotBlank(dto.getEvp_big())) {
				query.select(dto.getEvp_big());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("datas", list);
				return map;
			}
			if (StringUtils.isNotBlank(dto.getVis_hor_10mi())) {
				query.select(dto.getVis_hor_10mi());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("datas", list);
				return map;
			}

			if (StringUtils.isNotBlank(dto.getClo_cov())) {
				query.select(dto.getClo_cov());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("datas", list);
				return map;
			}
			if (StringUtils.isNotBlank(dto.getClo_cov_low())) {
				query.select(dto.getClo_cov_low());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("datas", list);
				return map;
			}

			return null;
	}

	@Override
	public Map<String, Object> getGroundAvgByFiled(DataAvgDto groundAvgDto) {

		Map<String, Object> map = new HashMap<String, Object>();
		DataAvgDto dto = groundAvgDto;
		QueryWrapper<GroundAvg> query = Wrappers.query();
		
		//处理地面数据   不同的数据类型   从不同的表中取
		if(StringUtils.isNotBlank(dto.getGround())){
			
			// 展示的数据 类型
			if (StringUtils.isNotBlank(dto.getHour())) {
				query.eq("data_type", DataTypeEnum.Hour.getValue());

				if (StringUtils.isNotBlank(dto.getDay_who_year())) {
					//query.eq("year", dto.getDay_who_year());
					//升序排列
					
					Map<String, Object> map1=this.getGroundHourByFiled(dto, dto.getDay_who_year());
					if(map1 !=null ){
						map.put("success", true);
						map.put("times", map1.get("times"));
						map.put("datas", map1.get("datas"));
					}else{
						map.put("success", false);
					}
					return map;

				} else {
					map.put("success", false);
					map.put("message", "查询日平均数据类型,后台没有接收到 查询年份参数");
					return map;
				}

			}else  if (StringUtils.isNotBlank(dto.getDay())) {
				query.eq("data_type", DataTypeEnum.Day.getValue());

				if (StringUtils.isNotBlank(dto.getDay_who_year())) {
					query.eq("year", dto.getDay_who_year());
					//升序排列
					List<String> listDate = groundAvgMapper.getDayTimeStr(DataTypeEnum.Day.getValue(),dto.getDay_who_year());

					//处理各种属性 查询
					Map<String, Object> map1=this.getMapByFilesDayMonWeekYear(dto);
					if(map1 !=null ){
						map.put("success", true);
						map.put("times", listDate);
						map.put("datas", map1.get("datas"));
					}else{
						map.put("success", false);
					}
					return map;

				} else {
					map.put("success", false);
					map.put("message", "查询日平均数据类型,后台没有接收到 查询年份参数");
					return map;
				}

			}  else if (StringUtils.isNotBlank(dto.getWeek())) {
				//统计  周平均数据的 处理
				query.eq("data_type", DataTypeEnum.Week.getValue());

				if (StringUtils.isNotBlank(dto.getDay_who_year())) {
					query.eq("year", dto.getDay_who_year());
					//升序排列
					List<String> listDate = groundAvgMapper.getWeekTimeStr(DataTypeEnum.Week.getValue(),dto.getDay_who_year());

					//处理各种属性 查询
					Map<String, Object> map1=this.getMapByFilesDayMonWeekYear(dto);
					if(map1 !=null ){
						map.put("success", true);
						map.put("times", listDate);
						map.put("datas", map1.get("datas"));
					}else{
						map.put("success", false);
					}
					return map;
				} else {
					map.put("success", false);
					map.put("message", "查询 周平均数据类型, 后台没有接收到 查询年份参数");
					return map;
				}
				
				

			} else if (StringUtils.isNotBlank(dto.getMon())) {
				//统计  月平均数据的 处理  月平均数据 展示所有的数据
				query.eq("data_type", DataTypeEnum.Mon.getValue());
				// 获取时间轴数据
				List<String> listDateMon = groundAvgMapper.getMonTimeStr(DataTypeEnum.Mon.getValue());
				
				//处理各种属性 查询
				Map<String, Object> map1=this.getMapByFilesDayMonWeekYear(dto);
				if(map1 !=null ){
					map.put("success", true);
					map.put("times", listDateMon);
					map.put("datas", map1.get("datas"));
				}else{
					map.put("success", false);
				}
				return map;

			}else if (StringUtils.isNotBlank(dto.getYear())) {
				
				//统计  年平均数据的 处理  月平均数据 展示所有的数据
				query.eq("data_type", DataTypeEnum.Year.getValue());
				
				// 获取时间轴数据
				List<String> listDateYear = groundAvgMapper.getYearTimeStr(DataTypeEnum.Year.getValue());
				Map<String, Object> map1=this.getMapByFilesDayMonWeekYear(dto);
				if(map1 !=null ){
					map.put("success", true);
					map.put("times", listDateYear);
					map.put("datas", map1.get("datas"));
				}else{
					map.put("success", false);
				}
				return map;

			}else {
				map.put("success", false);
				map.put("message", "查询地面  数据类型,后台没有接收到   统计何种  数据的 参数");
				return map;
			}			
		}
		return  null;
			
	}
	
	@Override
	public Map<String, Object> getGroundHourByFiled(DataAvgDto dataAvgDto,String yearStr) {
		Map<String, Object> map = new HashMap<String, Object>();
		DataAvgDto dto = dataAvgDto;
		QueryWrapper<GroundData> query = Wrappers.query();
		
		QueryWrapper<GroundData> queryTime = Wrappers.query();
		if (StringUtils.isNotBlank(dto.getPrs())) {
			query.select(dto.getPrs());
			query.gt(dto.getPrs(),0.0);  //大于0.0
			query.ne(dto.getPrs(), 999998.0000 );
			query.ne(dto.getPrs(), 999999.0000 );
			query.eq("year", yearStr);
			query.orderByAsc("data_time");
			List<Object> listData = groundDataMapper.selectObjs(query);
			
			
			queryTime.select("DATE_FORMAT(data_time,'%Y-%m-%d %H')");
			queryTime.gt(dto.getPrs(),0.0);  //大于0.0
			queryTime.ne(dto.getPrs(), 999998.0000 );
			queryTime.ne(dto.getPrs(), 999999.0000 );
			queryTime.eq("year", yearStr);
			queryTime.orderByAsc("data_time");
			List<Object> listTime = groundDataMapper.selectObjs(queryTime);
			
			map.put("datas", listData);
			map.put("times", listTime);
			return map;
		}

		if (StringUtils.isNotBlank(dto.getTem())) {
			query.select(dto.getTem());
			query.gt(dto.getTem(),0.0);  //大于0.0
			query.ne(dto.getTem(), 999998.0000 );
			query.ne(dto.getTem(), 999999.0000 );
			query.eq("year", yearStr);
			query.orderByAsc("data_time");
			List<Object> listData = groundDataMapper.selectObjs(query);
			
			
			queryTime.select("DATE_FORMAT(data_time,'%Y-%m-%d %H')");
			queryTime.gt(dto.getTem(),0.0);  //大于0.0
			queryTime.ne(dto.getTem(), 999998.0000 );
			queryTime.ne(dto.getTem(), 999999.0000 );
			queryTime.eq("year", yearStr);
			queryTime.orderByAsc("data_time");
			List<Object> listTime = groundDataMapper.selectObjs(queryTime);
			
			map.put("datas", listData);
			map.put("times", listTime);
			return map;
		}
		if (StringUtils.isNotBlank(dto.getDpt())) {
			query.select(dto.getDpt());
			query.gt(dto.getDpt(),0.0);  //大于0.0
			query.ne(dto.getDpt(), 999998.0000 );
			query.ne(dto.getDpt(), 999999.0000 );
			query.eq("year", yearStr);
			query.orderByAsc("data_time");
			List<Object> listData = groundDataMapper.selectObjs(query);
			
			
			queryTime.select("DATE_FORMAT(data_time,'%Y-%m-%d %H')");
			queryTime.gt(dto.getDpt(),0.0);  //大于0.0
			queryTime.ne(dto.getDpt(), 999998.0000 );
			queryTime.ne(dto.getDpt(), 999999.0000 );
			queryTime.eq("year", yearStr);
			queryTime.orderByAsc("data_time");
			List<Object> listTime = groundDataMapper.selectObjs(queryTime);
			
			map.put("datas", listData);
			map.put("times", listTime);
			return map;
		}

		if (StringUtils.isNotBlank(dto.getRhu())) {
			query.select(dto.getRhu());
			query.gt(dto.getRhu(),0.0);  //大于0.0
			query.ne(dto.getRhu(), 999998.0000 );
			query.ne(dto.getRhu(), 999999.0000 );
			query.eq("year", yearStr);
			query.orderByAsc("data_time");
			List<Object> listData = groundDataMapper.selectObjs(query);
			
			
			queryTime.select("DATE_FORMAT(data_time,'%Y-%m-%d %H')");
			queryTime.gt(dto.getRhu(),0.0);  //大于0.0
			queryTime.ne(dto.getRhu(), 999998.0000 );
			queryTime.ne(dto.getRhu(), 999999.0000 );
			queryTime.eq("year", yearStr);
			queryTime.orderByAsc("data_time");
			List<Object> listTime = groundDataMapper.selectObjs(queryTime);
			
			map.put("datas", listData);
			map.put("times", listTime);
			return map;
		}
		if (StringUtils.isNotBlank(dto.getVap())) {
			query.select(dto.getVap());
			query.gt(dto.getVap(),0.0);  //大于0.0
			query.ne(dto.getVap(), 999998.0000 );
			query.ne(dto.getVap(), 999999.0000 );
			query.eq("year", yearStr);
			query.orderByAsc("data_time");
			List<Object> listData = groundDataMapper.selectObjs(query);
			
			
			queryTime.select("DATE_FORMAT(data_time,'%Y-%m-%d %H')");
			queryTime.gt(dto.getVap(),0.0);  //大于0.0
			queryTime.ne(dto.getVap(), 999998.0000 );
			queryTime.ne(dto.getVap(), 999999.0000 );
			queryTime.eq("year", yearStr);
			queryTime.orderByAsc("data_time");
			List<Object> listTime = groundDataMapper.selectObjs(queryTime);
			
			map.put("datas", listData);
			map.put("times", listTime);
			return map;
		}

		if (StringUtils.isNotBlank(dto.getEvp_big())) {
			query.select(dto.getEvp_big());
			query.gt(dto.getEvp_big(),0.0);  //大于0.0
			query.ne(dto.getEvp_big(), 999998.0000 );
			query.ne(dto.getEvp_big(), 999999.0000 );
			query.eq("year", yearStr);
			query.orderByAsc("data_time");
			List<Object> listData = groundDataMapper.selectObjs(query);
			
			
			queryTime.select("DATE_FORMAT(data_time,'%Y-%m-%d %H')");
			queryTime.gt(dto.getEvp_big(),0.0);  //大于0.0
			queryTime.ne(dto.getEvp_big(), 999998.0000 );
			queryTime.ne(dto.getEvp_big(), 999999.0000 );
			queryTime.eq("year", yearStr);
			queryTime.orderByAsc("data_time");
			List<Object> listTime = groundDataMapper.selectObjs(queryTime);
			
			map.put("datas", listData);
			map.put("times", listTime);
			return map;
		}
		if (StringUtils.isNotBlank(dto.getVis_hor_10mi())) {
			query.select(dto.getVis_hor_10mi());
			query.gt(dto.getVis_hor_10mi(),0.0);  //大于0.0
			query.ne(dto.getVis_hor_10mi(), 999998.0000 );
			query.ne(dto.getVis_hor_10mi(), 999999.0000 );
			query.eq("year", yearStr);
			query.orderByAsc("data_time");
			List<Object> listData = groundDataMapper.selectObjs(query);
			
			
			queryTime.select("DATE_FORMAT(data_time,'%Y-%m-%d %H')");
			queryTime.gt(dto.getVis_hor_10mi(),0.0);  //大于0.0
			queryTime.ne(dto.getVis_hor_10mi(), 999998.0000 );
			queryTime.ne(dto.getVis_hor_10mi(), 999999.0000 );
			queryTime.eq("year", yearStr);
			queryTime.orderByAsc("data_time");
			List<Object> listTime = groundDataMapper.selectObjs(queryTime);
			
			map.put("datas", listData);
			map.put("times", listTime);
			return map;
		}

		if (StringUtils.isNotBlank(dto.getClo_cov())) {
			query.select(dto.getClo_cov());
			query.gt(dto.getClo_cov(),0.0);  //大于0.0
			query.ne(dto.getClo_cov(), 999998.0000 );
			query.ne(dto.getClo_cov(), 999999.0000 );
			query.eq("year", yearStr);
			query.orderByAsc("data_time");
			List<Object> listData = groundDataMapper.selectObjs(query);
			
			
			queryTime.select("DATE_FORMAT(data_time,'%Y-%m-%d %H')");
			queryTime.gt(dto.getClo_cov(),0.0);  //大于0.0
			queryTime.ne(dto.getClo_cov(), 999998.0000 );
			queryTime.ne(dto.getClo_cov(), 999999.0000 );
			queryTime.eq("year", yearStr);
			queryTime.orderByAsc("data_time");
			List<Object> listTime = groundDataMapper.selectObjs(queryTime);
			
			map.put("datas", listData);
			map.put("times", listTime);
			return map;
		}
		if (StringUtils.isNotBlank(dto.getClo_cov_low())) {
			query.select(dto.getClo_cov_low());
			query.gt(dto.getClo_cov_low(),0.0);  //大于0.0
			query.ne(dto.getClo_cov_low(), 999998.0000 );
			query.ne(dto.getClo_cov_low(), 999999.0000 );
			query.eq("year", yearStr);
			query.orderByAsc("data_time");
			List<Object> listData = groundDataMapper.selectObjs(query);
			
			
			queryTime.select("DATE_FORMAT(data_time,'%Y-%m-%d %H')");
			queryTime.gt(dto.getClo_cov_low(),0.0);  //大于0.0
			queryTime.ne(dto.getClo_cov_low(), 999998.0000 );
			queryTime.ne(dto.getClo_cov_low(), 999999.0000 );
			queryTime.eq("year", yearStr);
			queryTime.orderByAsc("data_time");
			List<Object> listTime = groundDataMapper.selectObjs(queryTime);
			
			map.put("datas", listData);
			map.put("times", listTime);
			return map;
		}

		return null;
		
	}	

	@Override
	public int saveAvgDay(Date dateStart,Date dateEnd) {
		//String strStart = "2010-01-01 00:00:00";
		//String strEnd = "2011-01-01 00:00:00";

		//Date dateStart = DateUtil.StringToDate(strStart, "yyyy-MM-dd HH:mm:ss");
		//Date dateEnd = DateUtil.StringToDate(strEnd, "yyyy-MM-dd HH:mm:ss");
		int flag = 0;
		while (true) {

			Map<String, String> map = WeatherDataUtil.getYearMonDayStr(dateStart);

			String result = groundAvgMapper.getGroundAvgDay(map.get("year"), map.get("mon"), map.get("day"));

			List<String> listStr = WeatherDataUtil.strToArrayList(result, "#");
			GroundAvg ga = new GroundAvg(listStr.get(0), listStr.get(1), listStr.get(2), listStr.get(3), listStr.get(4),
					listStr.get(5), listStr.get(6), listStr.get(7), listStr.get(8), listStr.get(9), listStr.get(10),
					listStr.get(11), listStr.get(12), listStr.get(13), listStr.get(14), listStr.get(15),
					listStr.get(16), listStr.get(17), listStr.get(18), listStr.get(19));
			ga.setDay_avg_id(IdWorker.getIdStr());
			ga.setCreate_time(DataUtil.getCurrenTimestamp());
			ga.setData_type("day");
			ga.setYear(map.get("year"));
			ga.setMon(map.get("mon"));
			ga.setDay(map.get("day"));
			ga.setDate_time(new Timestamp(dateStart.getTime()));
			ga.setDate_str(DateUtil.DateToString(dateStart, "yyyy-MM-dd"));

			Date nextDate = WeatherDataUtil.calculateEndDate(dateStart, 1);
			if (nextDate.after(dateEnd)) {
				break;
			}
			int jflag = groundAvgMapper.insert(ga);
			flag = flag + jflag;
			
			
			
			List<String> list=groundDataMapper.getIdByYearMonDay(map.get("year"), map.get("mon"), map.get("day"));
			if(list.size()>0){
				
				Iterator<String> iter=list.iterator();
				while(iter.hasNext()){
					String groundDataId=iter.next();
					
					GroundData gd=new GroundData();
					gd.setGround_data_id(groundDataId);
					gd.setAvg_flag(AvgFlagEnum.SUCCESS.getValue());
					//更新  ground de avgFlag
					groundDataMapper.updateById(gd);
				}
			}
			
			dateStart = nextDate;

		}

		//System.out.println("success 条" + flag);
		return flag;

	}
	@Override
	public int saveAvgMon(String year,String mon) {

			String result = groundAvgMapper.getGroundAvgMon(year,mon);

			
			String monStr=WeatherDataUtil.getMonMMStr(Integer.valueOf(mon));
			
			
			Date dateStart = DateUtil.StringToDate( year+"-"+monStr, "yyyy-MM");

			List<String> listStr = WeatherDataUtil.strToArrayList(result, "#");
			GroundAvg ga = new GroundAvg(listStr.get(0), listStr.get(1), listStr.get(2), listStr.get(3), listStr.get(4),
					listStr.get(5), listStr.get(6), listStr.get(7), listStr.get(8), listStr.get(9), listStr.get(10),
					listStr.get(11), listStr.get(12), listStr.get(13), listStr.get(14), listStr.get(15),
					listStr.get(16), listStr.get(17), listStr.get(18), listStr.get(19));
			ga.setDay_avg_id(IdWorker.getIdStr());
			ga.setCreate_time(DataUtil.getCurrenTimestamp());
			ga.setData_type(DataTypeEnum.Mon.getValue());
			ga.setYear(year);
			ga.setMon(String.valueOf(mon));
			ga.setDate_time(new Timestamp(dateStart.getTime()));
			ga.setDate_str(year+"-"+monStr);


			int jflag = groundAvgMapper.insert(ga);

		return jflag;

	}

	@Override
	public int saveAvgYear(String yearStr) {

		int flag = 0;


			String result = groundAvgMapper.getGroundAvgYear(yearStr);

			
			
			Date dateStart = DateUtil.StringToDate( yearStr,"yyyy");

			List<String> listStr = WeatherDataUtil.strToArrayList(result, "#");
			GroundAvg ga = new GroundAvg(listStr.get(0), listStr.get(1), listStr.get(2), listStr.get(3), listStr.get(4),
					listStr.get(5), listStr.get(6), listStr.get(7), listStr.get(8), listStr.get(9), listStr.get(10),
					listStr.get(11), listStr.get(12), listStr.get(13), listStr.get(14), listStr.get(15),
					listStr.get(16), listStr.get(17), listStr.get(18), listStr.get(19));
			ga.setDay_avg_id(IdWorker.getIdStr());
			ga.setCreate_time(DataUtil.getCurrenTimestamp());
			ga.setData_type(DataTypeEnum.Year.getValue());
			ga.setYear(yearStr);
			ga.setDate_str(yearStr);
			ga.setDate_time(new Timestamp(dateStart.getTime()));

			// Date nextDate=WeatherDataUtil.calculateEndDate(dateStart, 1);

			int jflag = groundAvgMapper.insert(ga);
			flag = flag + jflag;

			return jflag;
	}

	@Override
	public int saveAvgWeek(Date dateStart,Date dateEnd,String week) {
		//String strStart = "2009-01-01 00:00:00";
		//String strEnd = "2010-01-01 00:00:00";

		//Date dateStart = DateUtil.StringToDate(strStart, "yyyy-MM-dd HH:mm:ss");
		//Date dateEnd = DateUtil.StringToDate(strEnd, "yyyy-MM-dd HH:mm:ss");
		
		int flag = 1;
		//while (true) {
			//Date nextDate = WeatherDataUtil.calculateEndDate(dateStart, 7);
			//if (nextDate.after(dateEnd)) {
			//	break;
			//}
			Map<String, String> map = WeatherDataUtil.getYearMonDayStr(dateStart);

			String dateStartStr=DateUtil.DateToString(dateStart, "yyyy-MM-dd HH:mm:ss");
			String nextDateStr=DateUtil.DateToString(dateEnd, "yyyy-MM-dd HH:mm:ss");	
			
			String result = groundAvgMapper.getGroundAvgWeek(dateStartStr, nextDateStr);

			List<String> listStr = WeatherDataUtil.strToArrayList(result, "#");
			GroundAvg ga = new GroundAvg(listStr.get(0), listStr.get(1), listStr.get(2), listStr.get(3), listStr.get(4),
					listStr.get(5), listStr.get(6), listStr.get(7), listStr.get(8), listStr.get(9), listStr.get(10),
					listStr.get(11), listStr.get(12), listStr.get(13), listStr.get(14), listStr.get(15),
					listStr.get(16), listStr.get(17), listStr.get(18), listStr.get(19));
			ga.setDay_avg_id(IdWorker.getIdStr());
			ga.setCreate_time(DataUtil.getCurrenTimestamp());
			ga.setData_type(DataTypeEnum.Week.getValue());
			ga.setYear(map.get("year"));
			ga.setMon(map.get("mon"));
			ga.setDate_time(new Timestamp(dateStart.getTime()));
			ga.setDate_week(week);
			
			
			int jflag = groundAvgMapper.insert(ga);
			
			//dateStart = nextDate;

		//}

		//System.out.println("success 条" + flag);
		return jflag;
	}
	


	@Override
	public Map<String, Object> getGstNumberAvgByFiled(DataAvgDto dataAvgDto) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		DataAvgDto dto = dataAvgDto;
		QueryWrapper<GroundAvg> query = Wrappers.query();
		
		if (StringUtils.isNotBlank(dto.getDay_who_year())) {
			query.eq("year", dto.getDay_who_year());
			//升序排列
			List<String> listDateDays = groundAvgMapper.getDayTimeStr(DataTypeEnum.Day.getValue(),dto.getDay_who_year());
			
			map.put("times", listDateDays);
			map.put("success", true);
			
			if (StringUtils.isNotBlank(dto.getGst())) {
				query.select(dto.getGst());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("gst_datas", list);
			}
			if (StringUtils.isNotBlank(dto.getGst_5cm())) {
				query.select(dto.getGst_5cm());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("gst_5_datas", list);
			}
			if (StringUtils.isNotBlank(dto.getGst_10cm())) {
				query.select(dto.getGst_10cm());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("gst_10_datas", list);
			}
			if (StringUtils.isNotBlank(dto.getGst_15cm())) {
				query.select(dto.getGst_15cm());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("gst_15_datas", list);
			}
			if (StringUtils.isNotBlank(dto.getGst_20cm())) {
				query.select(dto.getGst_20cm());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("gst_20_datas", list);
			}
			if (StringUtils.isNotBlank(dto.getGst_40cm())) {
				query.select(dto.getGst_40cm());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("gst_40_datas", list);
			}
			if (StringUtils.isNotBlank(dto.getGst_80cm())) {
				query.select(dto.getGst());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("gst_80_datas", list);
			}
			if (StringUtils.isNotBlank(dto.getGst_160cm())) {
				query.select(dto.getGst_160cm());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("gst_160_datas", list);
			}
			if (StringUtils.isNotBlank(dto.getGst_320cm())) {
				query.select(dto.getGst_320cm());
				query.orderByAsc("date_time");
				List<Object> list = groundAvgMapper.selectObjs(query);
				map.put("gst_320_datas", list);
			}

			return map;

		} else {
			map.put("success", false);
			map.put("message", "后台没有接收到 查询年份参数");
			return map;
		}
	}
	
	


	@Override
	public Map<String, String> getDataTime() {
		
		Map<String, String> map=new HashMap<String,String>(); 
		String atimes=groundAvgMapper.getAerosolTimes();
		String gtimes=groundAvgMapper.getGroundTimes();
		String rTimes=groundAvgMapper.getRadiationTimes();
		String hTimes=groundAvgMapper.getHighTimes();
		
		map.put("ground", gtimes);
		map.put("aerosol", atimes);
		map.put("high", hTimes);
		map.put("radiation", rTimes);
		
		return map;
	}

	@Override
	public Map<String, Object> getMonTemCurrentYear() {
		
		Map<String, Object> map = new HashMap<String, Object>();
		Date newDate=new Date();
		String year=DateUtil.DateToString(newDate, "yyyy");
		
		//String year="2009";
		QueryWrapper<GroundAvg> query = Wrappers.query();
		//统计  月平均数据的 处理  月平均数据 展示所有的数据
		query.eq("data_type", DataTypeEnum.Mon.getValue());
		query.eq("year", year);
		// 获取时间轴数据
		List<String> listDateMon = groundAvgMapper.getCurrentYearMonTimeStr(DataTypeEnum.Mon.getValue(),year);
		//if (StringUtils.isNotBlank(dto.getTem())) {
			query.select("tem");
			List<Object> list = groundAvgMapper.selectObjs(query);
			map.put("times", listDateMon);
			map.put("datas", list);
			return map;
		//}
	}

	@Override
	public Map<String, Object> getNextWeekMessage() {
		
		Map<String, Object> map=new HashMap<String,Object>();
		
		GroundAvg ga=groundAvgMapper.getGroundAvgByDataType(DataTypeEnum.Week.getValue());
		
		if( ga !=null){
			String beforWeek=ga.getDate_week();
			String week="";
			
			String year=ga.getYear();
			String nestYear=Integer.valueOf(year)+1+""+"-01-01 00:00:00";
			Date nextYearStartDate=DateUtil.StringToDate(nestYear, "yyyy-MM-dd HH:mm:ss");
			
			Date startDate=WeatherDataUtil.calculateEndDate(new Date(ga.getDate_time().getTime()), 7);
			Date endDate=WeatherDataUtil.calculateEndDate(startDate, 7);
			
			//如果 日期跨年   下一个 开始日期  为下一年的   开始日期
			if(startDate.after(nextYearStartDate) || endDate.after(nextYearStartDate)){
				map.put("success", true);
				 startDate=nextYearStartDate;
				 endDate=WeatherDataUtil.calculateEndDate(startDate, 7);
				 beforWeek="第0周";
			}
			
			//探测  是否存在 下一个 周期的统计数据
			List<String> list=groundDataMapper.getIdByDataTime(DateUtil.DateToString(startDate, "yyyy-MM-dd HH:mm:ss"));
			if(list.size() >=7){
				 
				String weekStr=beforWeek.substring(beforWeek.indexOf("第")+1, beforWeek.indexOf("周"));
				int nextWeek=Integer.valueOf(weekStr)+1;
				week="第"+nextWeek+"周";
				map.put("success",true);
				map.put("message", "有满足条件的统计数据");
				map.put("startDate", startDate);
				map.put("endDate", endDate);
				map.put("whatWeek",week);
				return map;
				
			}else{
				map.put("success", false);
				map.put("message", "没有满足一周的统计数据,需等待 元数据产生");
				return map;
			}
		}else{
			Date startDate=DateUtil.StringToDate("1952-01-01 00:00:00", "yyyy-MM-dd HH:mm:ss");
			Date endDate=DateUtil.StringToDate("1952-01-08 00:00:00", "yyyy-MM-dd HH:mm:ss");
			// 需要 重头开始统计数据 
			map.put("success",true);
			map.put("message", "从 1952年 1月1号开始统计数据");
			map.put("startDate", startDate);
			map.put("endDate", endDate);
			map.put("whatWeek","第1周");
			return map;
		}
		
		
	}

	@Override
	public Map<String, Object> getNextMonMessage() {
		Map<String, Object> map=new HashMap<String,Object>();
		GroundAvg ga=groundAvgMapper.getGroundAvgByDataType(DataTypeEnum.Mon.getValue());
		
		if( ga !=null){
			//上次 日期的 年月
			String year=ga.getYear();
			String mon=ga.getMon();
			int monInt=Integer.valueOf(mon);
			int yearInt=Integer.valueOf(year);
			
			
			//查询  两个月后的日期
			Date towDate=WeatherDataUtil.calculateEndDate(new Date(ga.getDate_time().getTime()), 63);
			
			
			
			//探测  两个月 后  是否 存在 统计数据
			List<String> list=groundDataMapper.getIdByDataTime(DateUtil.DateToString(towDate, "yyyy-MM-dd HH:mm:ss"));
			if(list.size() >0){
				
				mon=monInt+1+"";
				
				if(monInt == 12){
					mon="1";
					year=yearInt+1+"";
				}
				
				
				map.put("success",true);
				map.put("message", "有满足条件的统计数据");
				map.put("year", year);
				map.put("mon", mon);
				return map;
				
			}else{
				map.put("success", false);
				map.put("message", "没有满足 下月 的统计数据,需等待 元数据产生");
				return map;
			}
		}else{
			// 需要 重头开始统计数据 
			map.put("success",true);
			map.put("message", "从 1952年 1月1号开始统计数据");
			map.put("year", "1951");
			map.put("mon", "1");
			return map;
		}
		
		
	}

	@Override
	public Map<String, Object> getNextYearMessage() {
		
		Map<String, Object> map=new HashMap<String,Object>();
		GroundAvg ga=groundAvgMapper.getGroundAvgByDataType(DataTypeEnum.Year.getValue());
		
		if( ga !=null){
			//上次 日期的 年月
			String year=ga.getYear();
			int yearInt=Integer.valueOf(year);
			
			
			//查询  下一年后的日期 365*2=730  366*2=732
			Date nextYearDate=WeatherDataUtil.calculateEndDate(new Date(ga.getDate_time().getTime()), 733);
			
			
			
			//探测  是否 存在 统计数据
			List<String> list=groundDataMapper.getIdByDataTime(DateUtil.DateToString(nextYearDate, "yyyy-MM-dd HH:mm:ss"));
			if(list.size() >0){
				
				year=yearInt+1+"";
				
				map.put("success",true);
				map.put("message", "有满足条件的统计数据");
				map.put("year", year);
				return map;
				
			}else{
				map.put("success", false);
				map.put("message", "没有满足 下一年  的统计数据,需等待 元数据产生");
				return map;
			}
		}else{
			// 需要 重头开始统计数据 
			map.put("success",true);
			map.put("message", "从 1952年 开始统计 年平均数据");
			map.put("year", "1952");
			return map;
		}
		
	}

	

	

}
